package com.lhy.service.impl;

import com.lhy.dao.MemberDao;
import com.lhy.dao.PaymentDetailsDao;
import com.lhy.dao.UserDao;
import com.lhy.domain.Member;
import com.lhy.domain.PaymentDetails;
import com.lhy.domain.User;
import com.lhy.service.MemberService;
import com.lhy.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 汪
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;
    @Autowired
    UserDao userDao;
    @Override
    public List<Member> showAllMember() {
        List<Member> members = memberDao.showAllMember();
        return members;
    }

    @Override
    public int addMember(Member member,User user) {
        user.setCreateDate(new Date());
        userDao.addUserAbdoutMember(user);
        List<User> someUser = userDao.findSomeUser(null, user.getRealName(), null);
        Integer id = someUser.get(0).getId();
        member.setUserId(id);
        memberDao.addMember(member);
        Member newMember=memberDao.getOneMemberByUserId(id);
        Integer memberId = newMember.getMemberId();
        userDao.updateMemberUser(memberId,id);

        return 0;
    }

    @Override
    public int modifyMember(Member member) {
        Integer userId=memberDao.getUserId(member.getMemberId());
        User user=new User();
        user.setId(userId);
        user.setRealName(member.getRealName());
        user.setTel(member.getTel());
        userDao.modifyNameAndTel(user);
        memberDao.modifyMember(member);
        return 0;
    }

    @Override
    public int deleteMember(int memberId) {
        Integer userId = memberDao.getUserId(memberId);
        memberDao.deleteMember(memberId);
        userDao.deleteUser(userId);

        return 0;
    }

    @Override
    public void modifyMount(Double rechargeMount,Integer memberId) {
        Member member=memberDao.getOneMemberByMemberId(memberId);
        memberDao.modifyMount(rechargeMount+member.getBalance(),memberId);
    }

    @Override
    public List<Member> showSomeMember(Pagination<Member> page) {
        return memberDao.showSomeMember(page);
    }
}
